﻿var tr1;
function init()
{
    searchMessage();
    
}




function searchMessage() {

    var searchString = document.getElementById("searchString").value;
    document.getElementById("tb_moreCity").style["visibility"] = 'hidden';
    $.getJSON('/City/searchButton/0,0,' + searchString, { t: new Date() }, function(j) {

        var value = j[0];
        writeMessage(value);

    })


}

function writeMessage(message) {
    //alert(message);
    var num = "";
    var values = message.split('|');

    $.getJSON('/City/initSearchTable/' + values[0] + "," + values[1] + ",1,20", { t: new Date() }, function(j) {
        document.getElementById("serachID").value = values[0];
        document.getElementById("resultCount").value = values[1];
        getMoreCityPageNum(j.length);
        document.getElementById("div_wait").style["visibility"] = 'visible';
        if (j.length < 20) {
            num = j.length;
        } else {
            num = 20;
        }
        for (var i = 0; i < num; i++) {

            //得到城市相应的信息
            var value = j[i];
            //alert(value);
            var arr = value.split('|');
            //得到商户名
            var shopName = arr[0];
            //得到城市的ID
            var cityID = arr[1];
            //得到商户ID
            var shopID = arr[2];
            //得到商户信息
            var shopMessage = arr[3];
            var message = shopMessage.substring(0, 68);
            //得到商户经纬度
            var shopLat = arr[4];
            var shopLng = arr[5];
            //得到商户的CATEGORYID
            var categoryID = arr[6];
            //得到商户的图片
            var shopLogo = arr[8];
            var shopType = "";

            //如果没有商户图片信息则进行默认图片显示
            if (shopLogo == "" || shopLogo == null) {
                shopLogo = '/Content/img/home/iniImg.jpg';
            }
            //商户的图片连接
            //var imgUrl = '/Content/img/shop/81/' + shopID + '/logo.jpg';

            //把相应的数据加载到相应的标签中
            $("#cityName" + (i + 1)).html(shopName);
            $("#message" + (i + 1)).html(message);
            $("#City" + (i + 1)).attr("title", shopName + "欢迎您");
            $("#City" + (i + 1)).attr("href", '/City/Trademark/' + shopID + '?CityID=' + cityID + '&CategoryID=' + categoryID + '&Lat=' + shopLat + '&Lng=' + shopLng);
            $("#firstCity" + (i + 1)).attr("href", '/City/Trademark/' + shopID + '?CityID=' + cityID + '&CategoryID=' + categoryID + '&Lat=' + shopLat + '&Lng=' + shopLng);
            $("#particularMessage" + (i + 1)).html("...>>>>>>详细信息");
            $("#img" + (i + 1)).attr("src", shopLogo);


            $(function() {
                $('#links tr').biggerlink();

                $('#links' + (i + 1) + ' tr').biggerlink();


            });


        }

        if (num < 20) {
            for (var t = num; t < 20; t++) {

                $("#links" + (t + 1)).hide();

            }
        }

        document.getElementById("div_wait").style["visibility"] = 'hidden';
        document.getElementById("tb_moreCity").style["visibility"] = 'visible';


    })

}



//得到更多城市里一共分多少页
function getMoreCityPageNum(cityNum) {

    //得到一共要分多少页

    tr1 = document.getElementById('moreCityPageNum').insertRow();
    //得到一共需要分成几页
    var s = cityNum / 20;
    var page = 0;

    page = parseInt(s);

    if (s > page) {
        page = page + 1;
    }
    //通过循环来把页码显示在页面上
    for (var j = 0; j < page; j++) {

        var td1 = tr1.insertCell();
        td1.width = "20px";
        td1.innerHTML = "<a href='#' onclick='pageNum(" + (j + 1) + ");' id='pageUrl'" + (j + 1) + " class='link'>" + (j + 1) + "</a>";
    }
}


//更多城市的分页程序
function pageNum(page) {
   
    var arr = new Array();
    var s = null;
    var cityNum = "";
    var resultCount = document.getElementById("resultCount").value;
    var searchID = document.getElementById("serachID").value;
    
    //通过JSON来得到相应城市信息
    $.getJSON('/City/initSearchTable/' + searchID + "," + resultCount + "," + page + ",20", { t: new Date() }, function(j) {

        if (j.length < 20) {
            cityNum = j.length;
        } else {
            cityNum = 20;
        }

        for (var i = 0; i < cityNum; i++) {

            //得到城市相应的信息
            var value = j[i];
            //alert(value);
            var arr = value.split('|');
            //得到商户名
            var shopName = arr[0];
            //得到城市的ID
            var cityID = arr[1];
            //得到商户ID
            var shopID = arr[2];
            //得到商户信息
            var shopMessage = arr[3];
            var message = shopMessage.substring(0, 68);
            //得到商户经纬度
            var shopLat = arr[4];
            var shopLng = arr[5];
            //得到商户的CATEGORYID
            var categoryID = arr[6];
            //得到商户的图片
            var shopLogo = arr[8];
            var shopType = "";

            //如果没有商户图片信息则进行默认图片显示
            if (shopLogo == "" || shopLogo == null) {
                shopLogo = '/Content/img/home/iniImg.jpg';
            }
            //商户的图片连接
            //var imgUrl = '/Content/img/shop/81/' + shopID + '/logo.jpg';

            //把相应的数据加载到相应的标签中
            $("#cityName" + (i + 1)).html(shopName);
            $("#message" + (i + 1)).html(message);
            $("#City" + (i + 1)).attr("title", shopName + "欢迎您");
            $("#City" + (i + 1)).attr("href", '/City/Trademark/' + shopID + '?CityID=' + cityID + '&CategoryID=' + categoryID + '&Lat=' + shopLat + '&Lng=' + shopLng);
            $("#firstCity" + (i + 1)).attr("href", '/City/Trademark/' + shopID + '?CityID=' + cityID + '&CategoryID=' + categoryID + '&Lat=' + shopLat + '&Lng=' + shopLng);
            $("#particularMessage" + (i + 1)).html("...>>>>>>详细信息");
            $("#img" + (i + 1)).attr("src", shopLogo);
            
            
            
             $(function() {
                    $('#links tr').biggerlink();

                    $('#links' + (i+1) + ' tr').biggerlink();


                });


        }

            if (cityNum < 20) {

                for (var t = cityNum; t < 20; t++) {

                    $("#links" + (t + 1)).hide();

                }
            }

        
    })
}